home *** CD-ROM | disk | FTP | other *** search
- Path: brtph500.bnr.ca!oophelp
- From: oophelp@bnr.ca (OOP Helpline)
- Newsgroups: comp.lang.c++
- Subject: Re: polymorphism and style
- Date: 29 Feb 1996 15:35:13 GMT
- Organization: Bell Northern Research
- Message-ID: <4h4h3h$6ur@brtph500.bnr.ca>
- References: <4gr4ur$n13@alpha.pcix.com> <DnI6II.KMt@news.arco.com>
- NNTP-Posting-Host: brtph89d.bnr.ca
- Originator: oophelp@brtph89d
-
-
- |> > Question on polymorphism and style.
- |> >Want to call a function in a most derived class off a pointer to the base
- |> >class. The fuction does not exist in the base class.
- |> >I should:
- |> >1)downcast ?
- |> >2)add the function to the base class and add virtual stubs to every
- |> > class that does not use it ?
- |> >3)add it to the base class but define it like
- |> > foo( ..whatever..) {error_msg("virtual function not defined");}
- |> > This way only the class that uses it would have to define it.
- |> >4) ?
- |> >
- |> >thanks for your time.
-
- |>but maybe you need to take another
- |> look at your design.
-
- Exactly, this is clearly a design fault. Upcasting a derived class object
- to a base class requires that you only do base class operations on it.
- (While any pilot can be a passenger on an airplane, any passenger cannot
- be a pilot.)
-
- If you need to keep the ability to do derived class behavior on a derived
- class object, you need to keep a derived class reference to it
- (i.e. use two pointers to the derived class object, one for use
- for base class behavior and one for use for derived class behavior).
-